﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class ReadMsg : System.Web.UI.Page
{
    private string userName;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["userName"]!=null)
        {
           userName=Session["userName"].ToString();
            if(!IsPostBack)
            {
            BindNoReadMsg();
            BindReadedMsg();
           }
        }
        else
        {
            Response.Redirect(GetUrlPath.GetUrl + "/Login.aspx");
        }

    }
    //读取未读信息,并显示未读的信息数量
    protected void BindNoReadMsg()
    {
        messages msg = new messages();
        DataSet ds = msg.SearchNoReadMsg();
        dgNoReadMsg.DataSource = ds.Tables["NoReadMessages"].DefaultView;
        dgNoReadMsg.DataBind();
                
        //调用message类的统计未读信息方法
        int count =Convert.ToInt32( msg.CountNoReadMsg());
        if(count==0)
        {
            lblNoReadMsg.Text = "<font color='red'>0</font>";
            dgNoReadMsg.Visible = false;

        }
        else
        {
            lblNoReadMsg.Text = "<font color='red'>"+count+"</font>";
        }

    }
    //读取已经阅读过的信息
    protected void BindReadedMsg()
    {
        messages msg = new messages();
        DataSet ds = msg.SearchReadedMsg();
        dgReadedMsg.DataSource = ds.Tables["readedMessage"];
        dgReadedMsg.Columns[0].Visible = false;
        dgReadedMsg.DataBind();
        //调用message类统计已经阅读的信息方法
        int count = msg.CountReadedMsg();
        if (count == 0)
        {
          lblReadedCount.Text = "<font color='red'>0</font>";
          dgReadedMsg.Visible = false;
        }
        else
        {
            lblReadedCount.Text = "<font color='red'>" + count + "</font>";
        }

    }
    //分页
    protected void dgNoReadMsg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgNoReadMsg.CurrentPageIndex = e.NewPageIndex;
        BindNoReadMsg();

    }
    //分页
    protected void dgReadedMsg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgReadedMsg.CurrentPageIndex = e.NewPageIndex;
        BindReadedMsg();
    }

    //在创建行时，给每一行模板列里的删除按钮添加客户端确认对话框
    protected void dgReadedMsg_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.EditItem:
            case ListItemType.AlternatingItem:
                Button btnDel = (Button)e.Item.FindControl("btnDelete");
                btnDel.Attributes.Add("onClick", "return confirm('您真的要删除这条信息吗？');");
                break;
        }
    }
    //实现删除信息
    protected void dgReadedMsg_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if(e.CommandName=="deleteMsg")
        {
            if (dgReadedMsg.Items.Count == 1)
            {
                if (dgReadedMsg.CurrentPageIndex != 0)
                {
                    dgReadedMsg.CurrentPageIndex = dgReadedMsg.CurrentPageIndex - 1;
                }
            }
             messages msg = new messages();
             bool seccess = msg.DeleteMsg(Convert.ToInt32( e.Item.Cells[0].Text));
             BindReadedMsg();
           
        }
    }

    protected void dgNoReadMsg_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                Label lblTrueName = (Label)e.Item.FindControl("lblTrueName");
                lblTrueName.Text = BasicClass.GetTrueNameByUserName(e.Item.Cells[0].Text);
                break;
        }
    }
    protected void dgReadedMsg_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                Label lblSendName = (Label)e.Item.FindControl("lblSendName");
                lblSendName.Text = BasicClass.GetTrueNameByUserName(e.Item.Cells[1].Text);
                break;
        }
    }
}
